% Monetary policy VAR analysis: Empirical 

% This program is based on VAR toolbox 2.0 by Ambrogio Cesa Bianchi

% The VAR Toolbox 2.0 is required to run this code. To get the 
% latest version of the toolboxes visit: 
% 
%       https://sites.google.com/site/ambropo/MatlabCodes
% % =======================================================================
% Ambrogio Cesa Bianchi, March 2015
% ambrogio.cesabianchi@gmail.com


%% PRELIMINARIES
% =======================================================================
%clear all; clear session; close all; clc
%warning off all

%_TFP specification_* %
tfp_sp=0;

%% Load data

if tfp_sp==0
[xlsdata, xlstext] = xlsread('VAR_level_data.xlsx','hours_noA_price'); % With price level data


else
[xlsdata, xlstext] = xlsread('VAR_data.xlsx','hours');  


end 

% Define and transform (if needed)
X = xlsdata;
%vnames = xlstext(1,2:end);


%% VAR ESTIMATION
% =======================================================================
% Define number of variables and of observations
[nobs, nvar] = size(X);
det = 1;
% Set number of nlags
nlags = 4;
% Estimate VAR
[VAR, VARopt] = VARmodel(X,nlags,det);


%% COMPUTE IRF AND FEVD
% =======================================================================
% Set options some options for IRF calculation
VARopt.nsteps = 63; %50
VARopt.ident = 'oir';
%VARopt.vnames = vnames;
% Compute IRF
[IRF, VAR] = VARir(VAR,VARopt);
% Compute error bands
[IRFINF,IRFSUP,IRFMED, IRFBAR] = VARirband(VAR,VARopt);
[pIRFINF,pIRFSUP,pIRFMED, pIRFBAR] = VARirband_price_level(VAR,VARopt);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Creating IRF variables WITHOUT TFP%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if tfp_sp == 0

diffIRFINF_pai= [0;diff(IRFINF(1:62,3,6))];
diffIRFSUP_pai= [0;diff(IRFSUP(1:62,3,6))];
diffIRFBAR_pai= [0;diff(IRFBAR(1:62,3,6))];
pIRFINF_pai=[0;pIRFINF(:,3,6)];
pIRFSUP_pai=[0;pIRFSUP(:,3,6)];
pIRFBAR_pai=[0;pIRFBAR(:,3,6)];

inf_mn=pIRFBAR_pai;
inf_hb=pIRFSUP_pai;
inf_lb=pIRFINF_pai;
y_mn=IRFBAR(:,1,6);
y_hb=IRFSUP(:,1,6);
y_lb=IRFINF(:,1,6);
i_mn=IRFBAR(:,6,6);
i_hb=IRFSUP(:,6,6);
i_lb=IRFINF(:,6,6);
c_mn=IRFBAR(:,2,6);
c_hb=IRFSUP(:,2,6);
c_lb=IRFINF(:,2,6);
w_mn=IRFBAR(:,4,6);
w_hb=IRFSUP(:,4,6);
w_lb=IRFINF(:,4,6);
h_mn=IRFBAR(:,5,6);
h_hb=IRFSUP(:,5,6);
h_lb=IRFINF(:,5,6);


else

diffIRFINF_pai= [0;diff(IRFINF(1:62,3,6))];
diffIRFSUP_pai= [0;diff(IRFSUP(1:62,3,6))];
diffIRFBAR_pai= [0;diff(IRFBAR(1:62,3,6))];
pIRFINF_pai=[0;pIRFINF(:,3,7)];
pIRFSUP_pai=[0;pIRFSUP(:,3,7)];
pIRFBAR_pai=[0;pIRFBAR(:,3,7)];


inf_mn=pIRFBAR_pai;
inf_hb=pIRFSUP_pai;
inf_lb=pIRFINF_pai;
y_mn=IRFBAR(:,1,7);
y_hb=IRFSUP(:,1,7);
y_lb=IRFINF(:,1,7);
i_mn=IRFBAR(:,7,7);
i_hb=IRFSUP(:,7,7);
i_lb=IRFINF(:,7,7);
c_mn=IRFBAR(:,2,7);
c_hb=IRFSUP(:,2,7);
c_lb=IRFINF(:,2,7);
w_mn=IRFBAR(:,4,7);
w_hb=IRFSUP(:,4,7);
w_lb=IRFINF(:,4,7);
h_mn=IRFBAR(:,5,7);
h_hb=IRFSUP(:,5,7);
h_lb=IRFINF(:,5,7);
tfp_mn=IRFBAR(:,6,7);
tfp_hb=IRFSUP(:,6,7);
tfp_lb=IRFINF(:,6,7);

end



figure(100)

if tfp_sp==0
    
subplot(3,2,1)
plot(1:62,pIRFINF_pai(1:62),':'); hold on;  plot(1:62,pIRFSUP_pai(1:62),':'); hold on; plot(1:62,pIRFBAR_pai(1:62),'-') ; 

title('inf');
hold on

subplot(3,2,2)
plot(1:62,IRFINF(1:62,1,6),':'); hold on;  plot(1:62,IRFSUP(1:62,1,6),':'); hold on; plot(1:62,IRFBAR(1:62,1,6),'-') ; 

title('y');
hold on

subplot(3,2,3)
plot(1:62,IRFINF(1:62,2,6),':'); hold on;  plot(1:62,IRFSUP(1:62,2,6),':'); hold on; plot(1:62,IRFBAR(1:62,2,6),'-') ; 
title('c');
hold on

subplot(3,2,4)
plot(1:62,IRFINF(1:62,6,6),':'); hold on;  plot(1:62,IRFSUP(1:62,6,6),':'); hold on; plot(1:62,IRFBAR(1:62,6,6),'-') ; 
title('i');
hold on

else

subplot(3,2,1)
plot(1:62,pIRFINF_pai(1:62),':'); hold on;  plot(1:62,pIRFSUP_pai(1:62),':'); hold on; plot(1:62,pIRFBAR_pai(1:62),'-') ; 

title('inf');
hold on

subplot(3,2,2)
plot(1:62,IRFINF(1:62,1,7),':'); hold on;  plot(1:62,IRFSUP(1:62,1,7),':'); hold on; plot(1:62,IRFBAR(1:62,1,7),'-') ; 

title('y');
hold on

subplot(3,2,3)
plot(1:62,IRFINF(1:62,2,7),':'); hold on;  plot(1:62,IRFSUP(1:62,2,7),':'); hold on; plot(1:62,IRFBAR(1:62,2,7),'-') ; 
title('c');
hold on

subplot(3,2,4)
plot(1:62,IRFINF(1:62,7,7),':'); hold on;  plot(1:62,IRFSUP(1:62,7,7),':'); hold on; plot(1:62,IRFBAR(1:62,7,7),'-') ; 
title('i');
hold on


subplot(3,2,5)
plot(1:62,IRFINF(1:62,6,7),':'); hold on;  plot(1:62,IRFSUP(1:62,6,7),':'); hold on; plot(1:62,IRFBAR(1:62,6,7),'-') ; 
title('TFP');
hold on


end

